package org.eae.eldf.webapp.controller.module;


import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.eae.eldf.dto.module.ModuleInfoQueryDto;
import org.eae.eldf.entity.module.ModuleInfo;
import org.eae.eldf.entity.sys.SysOperator;
import org.eae.eldf.framework.annotation.Token;
import org.eae.eldf.framework.persistence.Criteria;
import org.eae.eldf.framework.spring.SpringSecurityUtils;
import org.eae.eldf.service.module.ModuleInfoService;
import org.eae.eldf.webapp.base.AbstractController;
import org.eae.eldf.webapp.common.bean.Result;
import org.eae.eldf.webapp.common.bean.ResultList;
import org.eae.eldf.webapp.common.constant.StateDefine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContext;
import org.eae.eldf.framework.pager.Pagination;


@Controller
@RequestMapping("/module")
public class ModuleInfoController extends AbstractController<ModuleInfo>{

	private ModuleInfoService moduleInfoService;
	
	private EhCacheCacheManager cacheManager;
	
	@Autowired
	public void setModuleInfoService(ModuleInfoService moduleInfoService) {
		this.moduleInfoService = moduleInfoService;
	}
	@Autowired
	public void setCacheManager(EhCacheCacheManager cacheManager) {
		this.cacheManager = cacheManager;
	}
	
	/**
	 * 查询
	 * @param searchValue
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/info")
	public String initPage(@RequestParam(required = false) String searchValue, ModelMap model,
			HttpServletRequest request, HttpServletResponse response){
		
		return "/module/info/module_info";
	}
	/**
	 * 分页查询
	 * @param request
	 * @param response
	 * @param page
	 * @param rows
	 * @param searchValue
	 * @return
	 */
	@RequestMapping(value = "/info/list")
	public  @ResponseBody ResultList<ModuleInfo> pageList(
			HttpServletRequest request, 
			HttpServletResponse response,
			@ModelAttribute(value="moduleInfoQueryDto") ModuleInfoQueryDto moduleInfoQueryDto ){
		Pagination<ModuleInfo> pager = super.newPaginationInstance(moduleInfoQueryDto);
		pager = moduleInfoService.queryPageModuleList(pager,moduleInfoQueryDto);
		return outPageArrayList(pager,moduleInfoQueryDto);
	}
	
	/**
	 * 删除模块
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/info/delete")
	public  @ResponseBody
	Result delete(@ModelAttribute(value="moduleInfo") ModuleInfo moduleInfo ) {
		Result result = new Result(StateDefine.FLAG_SUCCESS, true);
		moduleInfoService.delete(moduleInfo.getModuleInfoId());
		return result;
	}
	
	/**
	 * 新增模块
	 * @Date        :      2016年10月20日
	 * @param request
	 * @param moduleInfo
	 * @param moduleInfoId
	 * @return
	 */
	@RequestMapping(value = "/info/add", method = RequestMethod.POST)
/*	@Token(remove = true, repeatSubmitForwardURL = "/module/info/form")*/
	@ResponseBody
	public Result insert(
			HttpServletRequest request,
			@ModelAttribute(value="moduleInfo") ModuleInfo moduleInfo,
			@RequestParam(required = false) String moduleInfoId
			/*@RequestParam(required = false) String moduleCode,
			@RequestParam(required = false) String moduleName,
			@RequestParam(required = false) String moduleDesc,
			@RequestParam(required = false) String moduleStatus,
			@RequestParam(required = false) String moduleUrl,
			@RequestParam(required = false) Integer moudleIsDelete,
			@RequestParam(required = false) Integer isDefault,
			@RequestParam(required = false) String i18n_key,
			HttpServletRequest request, ModelAndView model*/) {
	/*	System.out.println(moduleCode);
				ModuleInfo moduleInfo = new ModuleInfo();
				moduleInfo.setModuleName(moduleName);
				moduleInfo.setModuleCode(moduleCode);
				//moduleInfo.setModuleStatus(moduleStatus);
				moduleInfo.setModuleUrl(moduleUrl);
				moduleInfo.setModuleDesc(moduleDesc);
				moduleInfo.setModuleIsDelete(0);
				moduleInfo.setCreateTime(new Date());
				moduleInfo.setCreateUser(SpringSecurityUtils
						.getCurrentUserName());
				moduleInfo.setIsDefault(isDefault);
				moduleInfo.setI18n_key(i18n_key);
				moduleInfoService.insert(moduleInfo);
				model.addObject("success", true);
				model.setViewName("forward:/module/info/form");

				return model;*/
		RequestContext requestContext = new RequestContext(request);
		Result result = new Result(StateDefine.FLAG_SUCCESS, true);
		//判断用户名是否已经存在
		if(moduleInfoService.queryCount(Criteria.create(ModuleInfo.class).where("moduleCode", new Object[]{moduleInfo.getModuleInfoId()}))>0){
			result.setResultValue(false);
			result.setMessage(requestContext.getMessage("moduleinfo.msg.moduleCode.exist"));
		}else{
			//moduleInfo.setModuleStatus(moduleStatus);
			moduleInfo.setModuleIsDelete(0);
			moduleInfo.setCreateTime(new Date());
			moduleInfo.setCreateUser(SpringSecurityUtils
					.getCurrentUserName());
			moduleInfoService.insert(moduleInfo);
		} 
	
		return result;
	}
	
	/**
	 * 编辑模块
	 * @param id
	 * @param moduleInfoId
	 * @param moduleCode
	 * @param moduleName
	 * @param moduleDesc
	 * @param moduleStatus
	 * @param moduleUrl
	 * @param moudleIsDelete
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/info/update", method = RequestMethod.POST)
	@ResponseBody
	public Result update(
			/*@RequestParam(required = false) Long id,
			@RequestParam(required = false) String moduleInfoId,
			@RequestParam(required = false) String moduleCode,
			@RequestParam(required = false) String moduleName,
			@RequestParam(required = false) String moduleDesc,
			@RequestParam(required = false) String moduleStatus,
			@RequestParam(required = false) String moduleUrl,
			@RequestParam(required = false) Integer moudleIsDelete,
			@RequestParam(required = false) Integer isDefault,
			@RequestParam(required = false) String i18n_key,
			 ModelAndView model*/
			HttpServletRequest request,
			@ModelAttribute(value="moduleInfo") ModuleInfo moduleInfo,
			@RequestParam(required = false) String moduleInfoId
			) {
		Result result = new Result(StateDefine.FLAG_SUCCESS, true);
		Date lastUpdateTime	 = 	new Date();	
		String lastUpdateUser =SpringSecurityUtils
				.getCurrentUserName(); 
		
		moduleInfoService.update(Criteria.create(ModuleInfo.class)
				.set("moduleName", moduleInfo.getModuleName())
				.set("moduleDesc", moduleInfo.getModuleDesc())
				.set("moduleCode", moduleInfo.getModuleCode())
			//	.set("moduleStatus", moduleStatus)
				.set("moduleUrl", moduleInfo.getModuleUrl())
				.set("lastUpdateTime", lastUpdateTime)
				.set("lastUpdateUser",lastUpdateUser)
				.set("isDefault", moduleInfo.getIsDefault())
				.set("i18n_key", moduleInfo.getI18n_key())
				.where("moduleInfoId", new Object[] { moduleInfo.getModuleInfoId() }));
	/*	model.setViewName("forward:/module/info/form");// forward
		Date lastUpdateTime	 = 	new Date();	
		String lastUpdateUser =SpringSecurityUtils
				.getCurrentUserName(); 
		Criteria c = Criteria.create(ModuleInfo.class)
		.set("moduleName", moduleName).set("moduleDesc", moduleDesc)
		.set("moduleCode", moduleCode)
	//	.set("moduleStatus", moduleStatus)
		.set("moduleUrl", moduleUrl)
		.set("lastUpdateTime", lastUpdateTime).set("lastUpdateUser",lastUpdateUser)
		.set("isDefault", isDefault)
		.set("i18n_key", i18n_key)
		.where("moduleInfoId", new Object[] { id });
		moduleInfoService.updateModule(c, id);
		model.addObject("success", true);
		return model;*/
		return result;
	}

	/**
	 * 跳转到表单页面
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping("/info/form")
	@Token(save = true)
	public String toFormPage(@RequestParam(required = false) Long id,
			ModelMap model,
			@RequestParam(required = false) String searchValue,
			@RequestParam(required = false) String sorting_column_order,
			@RequestParam(required = false) String sorting_asc_desc,
			@RequestParam(required = false) String iDisplayStart) {
		ModuleInfo moduleInfo = null;
		if (null != id) {
			moduleInfo = moduleInfoService.queryById(id);
		} else {
			moduleInfo = new ModuleInfo();
		}
		
		model.addAttribute("module", moduleInfo);
		model.addAttribute("searchValue", searchValue);
		model.addAttribute("sorting_column_order", sorting_column_order);
		model.addAttribute("sorting_asc_desc", sorting_asc_desc);
		model.addAttribute("iDisplayStart", iDisplayStart);
		return "/module/info/form";
	}
		
}
